<?php
/**
 * Created by PhpStorm.
 * User: 宅十三
 * Date: 2021/11/22
 * Time: 14:57
 */
namespace app\demo\model;

use think\facade\Cache;
use think\Model;

class Category extends Model
{
    public static function getCateAll()
    {
        //所有分类
        $list = Category::getCate();
        //递归
        $all = Category::getNextCate($list->toArray(),0);
        return $all;
    }
    //递归方法
    public static function getNextCate($all,$pid)
    {
        $arr =[];
        foreach ($all as $v){
            if ($v['parentid'] == $pid){
                $arr[$v['id']] = $v;
                $arr[$v['id']]['next'] = Category::getNextCate($all,$v['id']);
            }
        }
        return $arr;
    }
    //查询所有分类信息
    public static function getCate()
    {
        if (Cache::has('cate')){
             $list = Cache::get('cate');
        }else{
            $list = Category::select();
            Cache::set('cate',$list,600);
        }
         return $list;

    }
    public static function getEr()
    {
        $er=Category::name('category')
            ->where('parentid','<>','0')
            ->column('parentid');
        return $er;
    }
    public static function getYr()
    {
        $er=Category::name('category')
            ->where('parentid','=','0')
            ->select();
        return $er;
    }
    public static function getCategory($where)
    {
        $list=Category::name('category')
            ->where($where)
            ->order('sort','asc')
            ->select();
        return $list;
    }
}